package pacmanreturns;

import java.awt.*;

public class Maze {
	public static final int WIDTH = 20;
	public static final int HEIGHT = 20;
	public static final int ROWS = 32;
	public static final int COLUMNS = 28;
	public static final Color COLOR = Color.BLUE;
	public static final int SPEED = 2;

	Game canvas;
	AudioManager audio;

	public Maze(Game inCanvas, AudioManager inAudio) {
		canvas = inCanvas;
		audio = inAudio;
	}

	public void draw() {
		for (int i = 0; i < ROWS; i++) {
			for (int j = 0; j < COLUMNS; j++) {
				Point point = getCartesian(j, i);
				if (walls[i][j] == 0) {
					canvas.setColor(Color.BLUE);
					canvas.rectangle(point.x, point.y, WIDTH, HEIGHT);
				}
				if (walls[i][j] == 2) {
					canvas.setColor(new Color(255, 184, 151));
					canvas.rectangle(point.x + 8, point.y + 8, 5, 5);
				}
			}
		}
	}

	public Point getCartesian(int row, int column) {
		int x = row * WIDTH;
		int y = column * HEIGHT;
		return new Point(x, y);
	}

	public Point getCartesian(Point inPoint) {
		return getCartesian(inPoint.x, inPoint.y);
	}

	public Point getGrid(int x, int y) {
		int row = x / WIDTH;
		int column = y / HEIGHT;
		return new Point(row, column);
	}

	public Point getGrid(Point inPoint) {
		return getGrid(inPoint.x, inPoint.y);
	}

	public void setStart(Sprite sprite) {
		Point startCoordinates = getCartesian(start);
		sprite.setLocation(startCoordinates);
	}

	public boolean canGo(int direction, int x, int y) {
		switch (direction) {
			case Game.RIGHT:
			case Game.LEFT:
				if (y % HEIGHT == 0) {
					return isSpace(direction, x, y);
				}
				break;

			case Game.UP:
			case Game.DOWN:
				if (x % WIDTH == 0) {
					return isSpace(direction, x, y);
				}
				break;
			default: break;
		}
		return false;
	}

	public boolean isSpace(int direction, int x, int y) {
		switch (direction) {
			case Game.RIGHT: x += 20; break;
			case Game.UP:    y -= SPEED; break;
			case Game.LEFT:  x -= SPEED; break;
			case Game.DOWN:  y += 20; break;
			default: break;
		}

		Point p = getGrid(x, y);
		if (!checkGridBounds(p) || walls[p.y][p.x] > 0) {
			return true;
		}
		return false;
	}

	public void removeDot(int direction, int x, int y) {
		switch (direction) {
			case Game.UP:    y += 20; break;
			case Game.LEFT:  x += 20; break;
			default: break;
		}

		Point p = getGrid(x, y);
		if (!checkGridBounds(p) || walls[p.y][p.x] != 2) {
			return;
		} else {
			walls[p.y][p.x] = 1;
			audio.chompDot();
		}
	}

	private boolean checkGridBounds(Point p) {
		if (p.x < 0 || p.x >= COLUMNS || p.y < 0 || p.y >= ROWS) {
			return false;
		}
		return true;
	}

	private Point start = new Point(14, 24);

	private byte[][] walls = {
		{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
		{0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0},
		{0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0},
		{0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0},
		{0, 2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0},
		{0, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0},
		{0, 2, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0},
		{0, 2, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 2, 0, 0, 0, 0, 2, 0},
		{0, 2, 2, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 0, 0, 2, 2, 2, 2, 0, 0, 2, 2, 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